package com.ruoyi.project.fusion.service;

import com.ruoyi.project.fusion.algorithms.*;
import com.ruoyi.project.fusion.domain.*;
import com.ruoyi.project.fusion.mapper.DataFusionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DataFusionService {
    @Autowired
    private DataFusionMapper dataFusionMapper;

    public List<HumanResourceScores> getHumanResourceScores(String chainType, String scenario) {
//        return new HumanResourceAlgorithm().compute(dataFusionMapper.getHumanResourceAggInfos(chainType, scenario));
        List<HumanResourceScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<HumanResourceScores> batchResults;
        do {
            batchResults = dataFusionMapper.getHumanResourceScoreAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<BusinessStatusScores> getBusinessStatusScores(String chainType, String scenario) {
//        return new BusinessStatusAlgorithm().compute(dataFusionMapper.getBusinessStatusAggInfos(chainType, scenario));
        List<BusinessStatusScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<BusinessStatusScores> batchResults;
        do {
            batchResults = dataFusionMapper.getBusinessStatusScoreAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<MaterialManagementScores> getMaterialManagementScores(String chainType, String scenario) {
//        return new MaterialManagementAlgorithm().compute(dataFusionMapper.getMaterialManagementAggInfos(chainType, scenario));
        List<MaterialManagementScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<MaterialManagementScores> batchResults;
        do {
            batchResults = dataFusionMapper.getMaterialManagementScoreAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<QualityScores> getQualityScores(String chainType, String scenario) {
//        return new QualityAlgorithm().compute(dataFusionMapper.getQualityAggInfos(chainType, scenario));
        List<QualityScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<QualityScores> batchResults;
        do {
            batchResults =  dataFusionMapper.getQualityScoresAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<PriceScores> getPriceScores(String chainType, String scenario) {
//        return new PriceAlgorithm().compute(dataFusionMapper.getPriceAggInfos(chainType, scenario));
        List<PriceScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<PriceScores> batchResults;
        do {
            batchResults =  dataFusionMapper.getPriceScoresAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<ServiceScores> getServiceScores(String chainType, String scenario) {
//        return new ServiceAlgorithm().compute(dataFusionMapper.getServiceAggInfos(chainType, scenario));
        List<ServiceScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<ServiceScores> batchResults;
        do {
            batchResults =  dataFusionMapper.getServiceScoreAggInfos(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<TechnologyScores> getTechnologyScores(String chainType, String scenario) {
//        return new TechnologyAlgorithm().compute(dataFusionMapper.getTechnologyAggInfo(chainType, scenario));
        List<TechnologyScores> allResults = new ArrayList<>();
        int pageSize = 1000;  // 每次查询的条数，可根据实际情况调整
        int offset = 0;

        List<TechnologyScores> batchResults;
        do {
            batchResults =  dataFusionMapper.getTechnologyScoreAggInfo(chainType,scenario,offset,pageSize);
            allResults.addAll(batchResults);
            offset += pageSize; // 移动查询起始点
        } while (!batchResults.isEmpty()); // 直到查询不到数据，说明所有数据已获取完

        return allResults;
    }

    public List<BusinessStatusScores> getPartBusinessStatusScores(String chainType, String scenario, String enterpriseName) {
//        List<BusinessStatusScores> compute = new BusinessStatusAlgorithm().compute(dataFusionMapper.getBusinessStatusAggInfos(chainType, scenario));
        List<BusinessStatusScores> compute = getBusinessStatusScores(chainType,scenario);
        List<BusinessStatusScores> list = new ArrayList<>();
        for (BusinessStatusScores businessStatusScores : compute) {
            if(Objects.equals(businessStatusScores.getEnterpriseName(), enterpriseName)){
                list.add(businessStatusScores);
            }
        }
        return list;
    }

    public List<MaterialManagementScores> getPartMaterialManagementScores(String chainType, String scenario, String userName) {
//        List<MaterialManagementScores> compute = new MaterialManagementAlgorithm().compute(dataFusionMapper.getMaterialManagementAggInfos(chainType, scenario));
        List<MaterialManagementScores> compute = getMaterialManagementScores(chainType,scenario);
        List<MaterialManagementScores> list = new ArrayList<>();
        for (MaterialManagementScores materialManagementScores : compute) {
            if(Objects.equals(materialManagementScores.getEnterpriseName(), userName)){
                list.add(materialManagementScores);
            }
        }
        return list;
    }

    public List<QualityScores> getPartQualityScores(String chainType, String scenario, String userName) {
//        List<QualityScores> compute = new QualityAlgorithm().compute(dataFusionMapper.getQualityAggInfos(chainType, scenario));
        List<QualityScores> compute = getQualityScores(chainType,scenario);
        List<QualityScores> list = new ArrayList<>();
        for (QualityScores qualityScores : compute) {
            if(Objects.equals(qualityScores.getEnterpriseName(), userName)){
                list.add(qualityScores);
            }
        }
        return list;
    }

    public List<PriceScores> getPartPriceScores(String chainType, String scenario,String userName) {
//        List<PriceScores> compute = new PriceAlgorithm().compute(dataFusionMapper.getPriceAggInfos(chainType, scenario));
        List<PriceScores> compute = getPriceScores(chainType,scenario);
        List<PriceScores> list = new ArrayList<>();
        for (PriceScores priceScores : compute) {
            if(Objects.equals(priceScores.getEnterpriseName(), userName)){
                list.add(priceScores);
            }
        }
        return list;
    }

    public List<ServiceScores> getPartServiceScores(String chainType, String scenario, String userName) {
//        List<ServiceScores> compute = new ServiceAlgorithm().compute(dataFusionMapper.getServiceAggInfos(chainType, scenario));
        List<ServiceScores> compute = getServiceScores(chainType,scenario);
        List<ServiceScores> list = new ArrayList<>();
        for (ServiceScores serviceScores : compute) {
            if(Objects.equals(serviceScores.getEnterpriseName(), userName)){
                list.add(serviceScores);
            }
        }
        return list;
    }
}
